package com.hqyj.ctxp.config;

import com.hqyj.ctxp.interceptor.JwtInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MyCrossOrigin implements WebMvcConfigurer {

    //拦截器
    @Autowired
    private JwtInterceptor jwtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/**");
        WebMvcConfigurer.super.addInterceptors(registry);
    }

    // 重写addCorsMappings方法
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // /**表示所有请求都支持跨域访问
                .allowedOrigins("*") // 所有的外部域都可跨域访问
                .allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS") // 哪些请求 需要跨域配置
                //.allowCredentials(true) // 是否支持跨域用户凭证
                .maxAge(300) // 超时时长设置为5分钟。时间单位是秒
                .allowedHeaders("*"); // 请求体的头部
    }
}
